<%@ taglib uri="/linuxbox/common.tld" prefix="common" %>
<%@ taglib uri="/linuxbox/Calendar.tld" prefix="Calendar" %>
<%@ page import="java.util.*,linuxbox.util.StringUtil,linuxbox.calendar.logic.*,linuxbox.calendar.bean.*,linuxbox.admin.logic.*" %>

<%
  int SEARCH_BY_DESC = 0;
  int SEARCH_BY_TITLE = 1;
  String SEARCH_WORDS = (String)session.getAttribute("SEARCH_WORDS");
  if ( SEARCH_WORDS == null )
    SEARCH_WORDS = "";
  else
    SEARCH_WORDS = SEARCH_WORDS.trim();
  tmpStr = (String)session.getAttribute("SEARCH_BY_TITLE");
  if ( tmpStr != null )
    SEARCH_BY_TITLE = Integer.parseInt(tmpStr);
  tmpStr = (String)session.getAttribute("SEARCH_BY_DESC");
  if ( tmpStr != null )
    SEARCH_BY_DESC = Integer.parseInt(tmpStr);

  String SEARCH_CATEGORY = "";
  tmpStr = (String)session.getAttribute("SEARCH_CATEGORY");
  if ( tmpStr != null )
    SEARCH_CATEGORY = tmpStr;

  String SORTBY = (String) session.getAttribute("calendar.SORTBY");
  if (SORTBY==null)
  {
    SORTBY = "fromtime";
    session.setAttribute("calendar.SORTBY", SORTBY);
  }

  boolean sortAsc = true;
  tmpStr = (String) session.getAttribute("calendar.ASC");
  if (tmpStr!=null && tmpStr.length() > 0)
    sortAsc = new Boolean(tmpStr).booleanValue();
  else
    session.setAttribute("calendar.ASC", "true");

  int curPage = 0;
  String PAGE = (String) request.getParameter("PAGE");
  if (PAGE != null)
    curPage = Integer.parseInt(PAGE);

  int entriesPerPage = sec.getConfigInt("calendar", "Event List", "Entries Per Page");
  Collection col = BoxCalHome.search(curID, curCalUserID, SEARCH_WORDS, SEARCH_BY_DESC|SEARCH_BY_TITLE, SEARCH_CATEGORY,
  									SORTBY, sortAsc, entriesPerPage*curPage, entriesPerPage);
  int totalPage = (col.size() / entriesPerPage) +1;
  Iterator it = null;
  if (col != null)
    it = col.iterator();

  String calendarJSP = BoxStatic.jspDir+"calendar/BoxCalendar.jsp";
  String calendarAction = BoxStatic.servletDir+"BoxCalendarAction";
%>


<table width="100%" border=0 cellpadding=0 cellspacing=0>
<TR><TD align="left">

<FORM METHOD=post NAME=Search ACTION="<%=BoxStatic.servletDir%>BoxCalendarAction">
  <input type="hidden" name="v" value="search">

<TABLE cellpadding=0 cellspacing=0 border=0 width="100%">
  <tr bgcolor=eeeeee>
    <td>
      <TABLE CELLPADDING=3 CELLSPACING=0 BORDER=0 width="100%">
        <TR BGCOLOR=#7EC1F3>
          <TD NOWRAP colspan=2>
            <FONT face="Arial,Helvetica" SIZE=+1>
              <B>Search Events</B></FONT>
          </TD>
        </TR>
        <TR>
          <TD valign=top>
            <FONT size=-1 face="Arial,Helvetica" >
            <B>Search words</B></FONT>&nbsp;<FONT face="Arial,Helvetica" SIZE=-2><I>(80 characters max)</I></FONT><BR>
            <INPUT type=text name="SEARCH_WORDS" MAXLENGTH=80 SIZE=30 value=<%=SEARCH_WORDS%> >
            <B><INPUT type=submit name=SubmitSearch value="Find"></B>
            <br>
            <FONT face="Arial,Helvetica" SIZE=-1>
            <input type=checkbox name="SEARCH_BY_TITLE" value="1"
            <%
              if (SEARCH_BY_TITLE != 0)
                out.print(" checked ");
            %>> Look in titles
            <input type=checkbox name="SEARCH_BY_DESC" value="2"
            <%
              if (SEARCH_BY_DESC != 0)
                out.print(" checked ");
            %>> Look in Descriptions
            </FONT>
          </TD>
        </TR>
   		  <tr>
          <td> Find in Event Category:
            <select name="SEARCH_CATEGORY">
              <Calendar:CalendarCatList selected="<%=SEARCH_CATEGORY%>" showsystemcat="true" showallcategories="true" userid="<%=curCalUserID%>"/>
            </select>
   		 		</td>
        </tr>
      </TABLE>
    </td>
  </tr>
</TABLE>
</FORM>
</td></tr>
<tr><td>
<TABLE width="100%"cellpadding=0 cellspacing=0 border=0>
  <TR>
    <TD>
      <TABLE width="100%" cellpadding=3 cellspacing=0 border=0>
        <TR>
          <TD align=left bgcolor=#eeeeee>
            <FONT size=+1 face="Arial,Helvetica">
            <b>
<%
  if (SEARCH_WORDS.length() == 0)
    out.print("Search Results");
  else
    out.print("Search Results for '" + SEARCH_WORDS + "'");
%>
            </b>
            </FONT>
          </TD>
        </TR>
      </TABLE>
    </TD>
  </TR>
<%
  if (SEARCH_WORDS.length() > 0 || SEARCH_CATEGORY.length() > 0)
  {
    if ( it != null && it.hasNext())
    {
    String[] headers = {"Date", "Time", "Subject",  "Delete"};
    String[] sortby = {"fromtime", "", "subject",  ""};
%>
	<tr>
  	<td align=right>
    	<common:Page curPage="<%=curPage%>" totalPage="<%=totalPage%>" action="<%=calendarJSP%>" />
  	</td>
	</tr>

  <TR align=center bgcolor=dddddd>
    <TD align=center>
      <TABLE width="100%" cellpadding=2 cellspacing=1 border=0>

		    <TR bgColor=#7EC1F3>
    		  <common:ListHeader headers="<%=headers%>" sortby="<%=sortby%>" curSortBy="<%=SORTBY%>" sortAsc="<%=sortAsc%>" action="<%=calendarAction%>" />
    		</TR>
      <common:Iteration name="entry" type="linuxbox.calendar.logic.BoxCalendarEntry" group="<%=it%>">
        <TR bgcolor=ffffff>
          <TD align=left width="1%" nowrap>
            <FONT size=-1>
            <a href="<%=BoxStatic.servletDir%>BoxCalendarAction?v=day&t=<%=entry.getFromTime().getTime()%>"><%=StringUtil.getDateTimeFormatString(sec.getTimeZone(), BoxStatic.SHORT_DATE_FORMAT, entry.getFromTime())%></a>
            </FONT>
          </TD>
          <TD align=left width="1%" nowrap>
            <FONT size=-1>
<%
						if (entry.getIsAllDay())
              out.print("All day");
            else
            	out.print(entry.getTimeRangeString(sec.getTimeZone()));
%>
            </FONT>
          </TD>
          <TD>
            <Calendar:CalendarEntry timezone="<%=sec.getTimeZone()%>" viewerid="<%=curID%>" showtime="false" showdelete="false" calendarentry="<%=entry%>"/>
          </TD>
          <TD align=center width="1%">
<%
					if (entry.getUserID() == curID)
          {
%>
            <a href="<%=BoxStatic.servletDir%>BoxCalendarAction?SubmitDelete=del&v=search&id=<%=entry.getID()%>"
            Onclick='return confirmDel("<%=entry.getSubject()+ " " + entry.getTimeRangeString(sec.getTimeZone())%>")'
            ALT="Delete">Delete</a>
<%
          }
          else
             out.print("&nbsp;");
%>
          </TD>
        </TR>
      </common:Iteration>
      </TABLE>
    </TD>
  </TR>
	<tr>
  	<td align=right>
    	<common:Page curPage="<%=curPage%>" totalPage="<%=totalPage%>" action="<%=calendarJSP%>" />
  	</td>
	</tr>


<%
    }
    else
    {
      out.print("<tr><td>No events were found!</td></tr>");
    }
  }
%>
  <TR bgcolor=ffffff>
    <TD bgcolor=ffffff align=center>
    <FONT color=ffffff> --- --- --- --- --- --- --- --- --- --- ---
    --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
    --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
    </FONT>
    </TD>
  </TR>
</TABLE>
</td></tr>
</table>
